package pers.hl.module_main.explore

import android.os.Bundle
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.LogUtils
import org.simple.eventbus.Subscriber
import pers.hl.common.base.BaseVBVMFragment
import pers.hl.common.base.IFragment
import pers.hl.lib_global.common.Const
import pers.hl.module_main.common.PageHelper
import pers.hl.module_main.databinding.FragmentExploreBinding
import pers.hl.module_main.main.MainFragment

/**
 *发现
 */
class ExploreFragment: BaseVBVMFragment<FragmentExploreBinding, ExploreViewModel>(), IFragment,
    MainFragment.OnBarIconClickListener {

    private lateinit var exploreAdapter: ExploreAdapter

    override fun initView(savedInstanceState: Bundle?) {
        initAdapter()
        initRefresh()
    }

    private fun initRefresh() {
        mBinding.refreshLayout.apply {
            setOnRefreshListener {
                LogUtils.d("下拉刷新")
                lazyLoadData()
            }
            setEnableLoadMore(false)
        }

    }

    private fun initAdapter() {
        val data: MutableList<ExploreLayoutBean> = mutableListOf()
        exploreAdapter = ExploreAdapter(this)
        mBinding.rvExplore.apply {
            layoutManager = LinearLayoutManager(context)
            adapter = exploreAdapter
        }
        exploreAdapter.notifyDataSetChanged()
    }

    override fun initData(savedInstanceState: Bundle?) {

    }

    /**
     * banner展示借阅top5
     */
    private fun loadBannerData() {
        mViewModel.apply {
            loadTopBooks().observe(this@ExploreFragment, Observer {
                val bannerBean = ExploreLayoutBean.BannerBean(it)
                exploreAdapter.addData(0, bannerBean)
                LogUtils.d("loadBannerData")
            })
        }

    }

    private fun loadTagBooks() {
        mViewModel.apply {
            loadTags().observe(this@ExploreFragment, Observer {
                hideLoading()
                if (it.isNullOrEmpty()) {
                    return@Observer
                }
                val titleBeans = mutableListOf<ExploreLayoutBean>()
                for (bookTag in it) {
                    val titleBean = ExploreLayoutBean.RvTitleBean(bookTag)
                    val books = bookTag.books
                    if (books.isNullOrEmpty()) {
                        continue
                    }
                    titleBeans.add(titleBean)
                    val bookBean = ExploreLayoutBean.RvBookContentBean(books)
                    titleBeans.add(bookBean)
                }
                exploreAdapter.addData(titleBeans)
                LogUtils.d("loadTagBooks")
            })
        }
    }

    override fun name(): String {
        return "发现"
    }

    override fun onTitleBarIconClick() {
        PageHelper.navToSearch(mContext)
    }

    override fun enableLazyLoad(): Boolean {
        return true
    }

    override fun lazyLoadData() {
        showLoading()
        clearData()
        loadBannerData()
        loadTagBooks()
    }

    private fun loadBookByLibId(libId: Int) {
        mViewModel.loadBookByLibId(libId).observe(this, Observer {
            LogUtils.d("loadBookByLibId ${it.size}")
        })
    }

    private fun clearData() {
        exploreAdapter.data.clear()
        exploreAdapter.notifyDataSetChanged()
    }

    override fun showLoading() {
        mBinding.refreshLayout.autoRefreshAnimationOnly()
    }

    override fun hideLoading() {
        mBinding.refreshLayout.finishRefresh()
    }

    @Subscriber(tag = Const.MessageEvent.EVENT_BOOK_INFO_CHANGE)
    fun onBookChanged(msg: String) {
        LogUtils.d("onBookChanged")
        lazyLoadData()
    }
}